/**
 * getting video metadata
 */

var metadataXHR;

function handleMetadataResult(doc)
{
	var metaId = doc.split("id\":\"")[1].split("\"")[0];
	var metaTitle = doc.split("title\":\"")[1].split("\"")[0];
	var metaMpaa = doc.split("mpaa\":\"")[1].split("\"")[0];
	var metaYear = doc.split("year\":")[1].split(",")[0];
	var metaGenre = doc.split("genre\":\"")[1].split("\"")[0];
	var metaStars = doc.split("average\":\"")[1].split("\"")[0];
	
	var dctk = connected.toolkit;
	connected.debug.print("id = "+metaId);
	connected.debug.print("title = "+metaTitle);
	connected.debug.print("mpaa = "+metaMpaa);
	connected.debug.print("year = "+metaYear);
	connected.debug.print("genre = "+metaGenre);
	connected.debug.print("stars = "+metaStars);
	
	popupTitleTxt = dctk.getElementById("popupTitleTxt");
	popupTitleTxt.text = metaTitle;
	
	mpaaImg = dctk.getElementById("mpaaImg");
	mpaaImg.img.src = "http://cdn-0.nflximg.com/us/pages/parental_ratings/rating_"+metaMpaa+".gif";
	
	releasedTxt = dctk.getElementById("releasedTxt");
	releasedTxt.text = metaYear;
	
	genreTxt = dctk.getElementById("genreTxt");
	genreTxt.text = metaGenre;
	
	descTxt = dctk.getElementById("descTxt");
	descTxt.text = metaDesc.split("<br>")[1];
	
	playThumb = dctk.getElementById("playThumb");
	playThumb.img.src = metaImg;
	
	var rating = Math.round((metaStars)*Math.pow(10,0))/Math.pow(10,0);
	var stars = dctk.getElementById("stars");
	stars.img.src = rating+".png";
	
}

function startMetadataRequest()
{
    metadataXHR = new XMLHttpRequest();
    metadataXHR.onreadystatechange = onMetadataXHRStateChange;
    metadataXHR.open("GET", metadataUrl, true);
	//metadataXHR.overrideMimeType('text/xml');
    metadataXHR.send(null);
}

function onMetadataXHRStateChange()
{
    if (metadataXHR.readyState == 4)
    {
        if (metadataXHR.status == 200)
        {
            //findMedia(metadataXHR.responseXML);
			handleMetadataResult(metadataXHR.responseText);
        }
    }
}